package com.eddie.principle.ocp;

/**
 * @author Eddie
 * @date 2022/4/27 17:06
 */
public class Ocp {
    public static void main(String[] args) {
        //使用看看存在的问题
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Triangle());
    }
}

/**
 * 用于绘图的类 [使用方 ]
 */
class GraphicEditor {
    /**
     * 根据Type绘制不同的图形
     *
     * @param s
     */
    public void drawShape(Shape s) {
        if (s.mType == 1) {
            drawRectangle(s);
        } else if (s.mType == 2) {
            drawCircle(s);
        } else if (s.mType == 3) {
            drawTriangle(s);
        }
    }

    public void drawRectangle(Shape r) {
        System.out.println("绘制矩形");
    }

    public void drawCircle(Shape r) {
        System.out.println("绘制圆形");
    }

    public void drawTriangle(Shape r) {
        System.out.println("绘制三角形");
    }
}

class Shape {
    int mType;
}

class Rectangle extends Shape {
    Rectangle() {
        super.mType = 1;
    }
}

class Circle extends Shape {
    Circle() {
        super.mType = 2;
    }
}

/**
 * 新增画三角形
 */
class Triangle extends Shape {
    Triangle() {
        super.mType = 3;
    }
}